package com.wifirecommenderap;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.util.Log;

public class WifiScanReceive extends BroadcastReceiver {
  private static final String TAG = "WiFiScanReceiver";
  ScanningService wifiT;

  public WifiScanReceive(ScanningService wifiT) {
    super();
    this.wifiT = wifiT;
    Log.i(TAG, "Wifi Scan Initiated...");
  }
  
  @Override
  public void onReceive(Context c, Intent intent) {
	// Scans for networks 
	  
    List<ScanResult> results = wifiT.wifi.getScanResults();

    // List out networks
    Iterator<ScanResult> itr = results.iterator();
    
    /*while (itr.hasNext()) {
        ScanResult element = itr.next();
        System.out.print("Result: " + element.SSID + "\n");
        allNetworks.add(element.SSID);
        
    }
    
    for (ScanResult result : results) {
      if (bestSignal == null
          || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
        bestSignal = result;
    }*/
/*
    String message = String.format("%s networks found. %s is the strongest.",
        results.size(), bestSignal.SSID);

    Log.d(TAG, "onReceive() message: " + message);
    */
    
    String dt;
    Date cal=Calendar.getInstance().getTime();
    dt=cal.toLocaleString();    
    
    Log.i(TAG, "time_stamp() message: " + dt);
    
    // Save Data to Database;
    DBAdapter db = new DBAdapter(wifiT);
    db.open();
    db.deleteAll();
    while (itr.hasNext()) {
        ScanResult element = itr.next();
        db.insertTitle(dt, element.SSID.toString(), Integer.toString(element.level));
        //Log.i(TAG, "Network Message: " + element.SSID.toString()); 
    }
    
    db.close();  
  }

}